<?php
/**
 * 简单检查菜单数据
 */

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 检查系统管理菜单 ===\n\n";
    
    // 1. 检查"系统管理"菜单本身
    echo "【1】系统管理菜单数据\n";
    $stmt = $pdo->query("SELECT * FROM system_menu WHERE id = 1");
    $sysMenu = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($sysMenu) {
        echo "  ID: {$sysMenu['id']}\n";
        echo "  标题: {$sysMenu['title']}\n";
        echo "  PID: {$sysMenu['pid']}\n";
        echo "  SORT: {$sysMenu['sort']}\n";
        echo "  STATUS: {$sysMenu['status']}\n";
        echo "  URL: {$sysMenu['url']}\n";
        echo "  NODE: {$sysMenu['node']}\n";
        echo "  ICON: " . ($sysMenu['icon'] ?: '(无)') . "\n";
    } else {
        echo "  ❌ 找不到 ID=1 的菜单！\n";
    }
    
    // 2. 检查子菜单
    echo "\n【2】系统管理的子菜单\n";
    $stmt = $pdo->query("SELECT id, title, status, url, node FROM system_menu WHERE pid = 1 ORDER BY sort DESC, id ASC");
    $subMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (empty($subMenus)) {
        echo "  ❌ 没有子菜单！这会导致菜单被过滤掉！\n";
    } else {
        echo "  子菜单数量: " . count($subMenus) . "\n";
        foreach ($subMenus as $sub) {
            echo "  ├─ {$sub['title']} (ID:{$sub['id']}, STATUS:{$sub['status']}, URL:{$sub['url']}, NODE:{$sub['node']})\n";
        }
    }
    
    // 3. 检查启用的子菜单
    echo "\n【3】启用状态的子菜单\n";
    $stmt = $pdo->query("SELECT COUNT(*) FROM system_menu WHERE pid = 1 AND status = 1");
    $enabledCount = $stmt->fetchColumn();
    
    echo "  启用的子菜单数量: {$enabledCount}\n";
    
    if ($enabledCount == 0) {
        echo "  ❌ 没有启用的子菜单！\n";
        echo "  ❌ 这是问题所在！一级菜单必须有至少一个启用的子菜单才能显示！\n";
    } else {
        echo "  ✓ 有 {$enabledCount} 个启用的子菜单\n";
    }
    
    // 4. 检查所有一级菜单
    echo "\n【4】所有一级菜单对比\n";
    $stmt = $pdo->query("
        SELECT m.id, m.title, m.sort, m.status, 
               (SELECT COUNT(*) FROM system_menu WHERE pid = m.id AND status = 1) as sub_count
        FROM system_menu m
        WHERE m.pid = 0
        ORDER BY m.sort DESC, m.id ASC
    ");
    $allTopMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($allTopMenus as $menu) {
        $status = $menu['status'] == 1 ? '启用' : '禁用';
        $willShow = ($menu['status'] == 1 && $menu['sub_count'] > 0) ? '✓ 会显示' : '❌ 不显示';
        
        echo sprintf("  %s (ID:%d, SORT:%d, STATUS:%s, 子菜单:%d) - %s\n",
            $menu['title'],
            $menu['id'],
            $menu['sort'],
            $status,
            $menu['sub_count'],
            $willShow
        );
    }
    
    // 5. 给出解决方案
    echo "\n【5】解决方案\n";
    
    if ($sysMenu['status'] == 0) {
        echo "  ❌ 系统管理菜单被禁用了！\n";
        echo "  解决方案：UPDATE system_menu SET status = 1 WHERE id = 1;\n";
    } elseif ($enabledCount == 0) {
        echo "  ❌ 系统管理没有启用的子菜单！\n";
        echo "  解决方案：启用至少一个子菜单\n";
        echo "  执行：UPDATE system_menu SET status = 1 WHERE pid = 1;\n";
    } else {
        echo "  ✓ 数据库配置正常\n";
        echo "  问题可能在：\n";
        echo "  1. 权限过滤（检查 AdminService::check）\n";
        echo "  2. 自定义过滤函数（检查 admin_menu_filter）\n";
        echo "  3. 前端缓存（清除浏览器缓存）\n";
    }
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

